use Test::More qw/no_plan/;
use Cwd;
use FindBin;
use lib Cwd::abs_path("$FindBin::Bin/../../../../lib");
use lib Cwd::abs_path("$FindBin::Bin/../../../../generate/MyProject/lib");
use strict;
use warnings;

if(!eval 'use Plack'){
    $::INC{'Plack/Request.pm'} = $::INC{'Plack/Response.pm'} = 1;
    no warnings;
    *Plack::Request::new = *Plack::Response::new = sub{};
}

use MyProject;
ok my $p = MyProject->Template->Parser->new;
MyProject->begin;
my %opt = (context => MyProject->app('MyWebApp')->new({}), auto_filter => [qw/html/]);

{
    my $form = <<_END_;
<input name="t" />
<input name="t" type="text" />
<input name="t" type="hidden" />
<input name="t" type="password" />

<input type="checkbox" name="c" value="&amp;1" checked />
<input type="checkbox" name="c" value="&amp;2" checked />

<input type="radio" name="r" value="&amp;1" checked />
<input type="radio" name="r" value="&amp;2" checked />

<select name="sel">
    <option> &amp;1 </option>
    <option value="&amp;2"></option>
</select>

<textarea name="ta"></textarea>
_END_
    
    my $htm = <<_END_;
TEST
[%- FILLINFORM params #TEST -%]
$form
[%- END -%]
_END_
    
    is $p->open(\$htm)->init(\%opt)->render, "TEST\n$form\n";
    
    
    is $p->open(\$htm)->init({stash => {params => {
        t => "<T>\n",
        c => ['&1', '&2'],
        r => '&2',
        sel => ['&1', '&2'],
        ta => "<TA>\n",
    }}, %opt})->render, <<_END_;
TEST
<input name="t" value="&lt;T&gt;
" />
<input name="t" type="text" value="&lt;T&gt;
" />
<input name="t" type="hidden" value="&lt;T&gt;
" />
<input name="t" type="password" />

<input type="checkbox" name="c" value="&amp;1" checked="checked" />
<input type="checkbox" name="c" value="&amp;2" checked="checked" />

<input type="radio" name="r" value="&amp;1" />
<input type="radio" name="r" value="&amp;2" checked="checked" />

<select name="sel">
    <option selected="selected"> &amp;1 </option>
    <option value="&amp;2" selected="selected"></option>
</select>

<textarea name="ta">&lt;TA&gt;
</textarea>

_END_
}

{
    my $htm = <<_END_;
[%- FILLINFORM params password=1 -%]
<input name="p" type="password" />
[%- END -%]
_END_
    
    is $p->open(\$htm)->init({stash => {params => {p => '***'}}, %opt})->render,
        qq{<input name="p" type="password" value="***" />\n};
    
}
